package com.example.nuc_ms_beta.ui.viewconstruction.moderfragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.utils.LogUtils;
import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.nucinnotice.NucInNotice_ListView;
import com.example.nuc_ms_beta.ui.activity.UrlShowActivity;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.NucInNoticeListViewAdapter;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * Created by 张博川 on 2016/9/28.
 * 校内通知
 */
public class NucInNoticeFragment extends Fragment {

    private View view;//主View
    //ListView
    private ListView nucinnoticefragment_listview;
    private XRefreshView nucinoticefragment_xrefreshview;
    private static long lastRefreshTime;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return view;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.nucinnoticefragment, container, false);
        nucinnoticefragment_listview = (ListView) view.findViewById(R.id.nucinnoticefragment_listview);
        nucinoticefragment_xrefreshview = (XRefreshView) view.findViewById(R.id.nucinoticefragment_xrefreshview);
        // 设置是否可以下拉刷新
        nucinoticefragment_xrefreshview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        nucinoticefragment_xrefreshview.setPullLoadEnable(true);
        // 设置上次刷新的时间
        nucinoticefragment_xrefreshview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        nucinoticefragment_xrefreshview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        nucinoticefragment_xrefreshview.setAutoRefresh(false);
        nucinoticefragment_xrefreshview.getBackground().setAlpha(100);
        nucinoticefragment_xrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        nucinoticefragment_xrefreshview.stopRefresh();
                        lastRefreshTime = nucinoticefragment_xrefreshview.getLastRefreshTime();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        nucinoticefragment_xrefreshview.stopLoadMore();
                    }
                }, 2000);
            }

            @Override
            public void onRelease(float direction) {
                super.onRelease(direction);
                if (direction > 0) {

                } else {

                }
            }
        });
        nucinoticefragment_xrefreshview.setOnAbsListViewScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                LogUtils.i("onScrollStateChanged");
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                LogUtils.i("onScroll");
            }
        });
        queryData();
    }

    public static NucInNoticeFragment getInstance() {
        NucInNoticeFragment fragment = new NucInNoticeFragment();
        return fragment;
    }

    /**
     * Get the Data for ListView
     */
    private void queryData() {
        BmobQuery<NucInNotice_ListView> query_listView = new BmobQuery<>();
        query_listView.findObjects(new FindListener<NucInNotice_ListView>() {

            @Override
            public void done(final List<NucInNotice_ListView> list, BmobException e) {
                if (e == null) {
                    nucinnoticefragment_listview.setAdapter(new NucInNoticeListViewAdapter(getContext(), list, R.layout.nucinnoticefragment_listview_item));
                    nucinnoticefragment_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(MyApplication.context, UrlShowActivity.class);
                            intent.putExtra("url", list.get(position).getListview_content_url());
                            startActivity(intent);
                        }
                    });
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
